<?php

/**
 * 仓库管理
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\Warehouse as WarehouseService;
use app\admin\validate\Warehouse as WarehouseValidate;

class Warehouse extends BaseController
{
    /**
     * 仓库列表
     * @param Request $request
     * @param WarehouseService $warehouseService
     * @param WarehouseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(Request $request, WarehouseService $warehouseService, WarehouseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] =  $request->param('name', null);
        $param['start_date'] =  $request->param('start_date', null);
        $param['end_date'] =  $request->param('end_date', null);

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $warehouseService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 获取仓库信息
     * @param Request $request
     * @param WarehouseService $warehouseService
     * @param WarehouseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo(Request $request, WarehouseService $warehouseService, WarehouseValidate $validate)
    {
        $checkData = $request->param();
        //        if (!$validate->scene('show')->check($checkData)) {
        //            return json(['code' => 1, 'message' => $validate->getError()]);
        //        }
        $param['id'] = $request->param('id', null);
        $arrData = $warehouseService->getInfo($param);
        return json($arrData);
    }

    /**
     * 删除仓库
     * @param Request $request
     * @param WarehouseValidate $validate
     * @param WarehouseService $warehouseService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request, WarehouseValidate $validate, WarehouseService $warehouseService)
    {
        $checkData = $request->param();
        //        if (!$validate->scene('delete')->check($checkData)) {
        //            return json(['code' => 1, 'message' => $validate->getError()]);
        //        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $warehouseService->delete($param);
        return json($arrData);
    }

    /**
     * 保存仓库
     * @param Request $request
     * @param WarehouseService $warehouseService
     * @param WarehouseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, WarehouseService $warehouseService, WarehouseValidate $validate)
    {
        $checkData = $request->param();
        $checkType = isset($checkData['id']) ? 'save' : 'create';
        if (!$validate->scene($checkType)->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] =  $request->param('id', null);
        $param['name'] =  $request->param('name', null);
        $param['country'] =  $request->param('country', null);
        $param['city'] =  $request->param('city', null);
        $param['state'] =  $request->param('state', null);
        $param['address'] =  $request->param('address', null);
        $param['remark'] =  $request->param('remark', null);
        $param['contact_name'] =  $request->param('contact_name', null);
        $param['contact_phone'] =  $request->param('contact_phone', null);
        $param['is_customs_clearance'] =  $request->param('is_customs_clearance', null);
        $param['transaction_currency_id'] = implode(',', $request->param('transaction_currency_id', null));
        $param['pickup_address'] = $request->param('pickup_address', null);
        $param['show_name'] = $request->param('show_name', null);
        $param['fl_warehouse_id'] = $request->param('fl_warehouse_id', null);
        $param['support_selfpickup'] = $request->param('support_selfpickup', 0);
        $param['status'] = $request->param('status', 1);

        $arrData = $warehouseService->save($param);
        return json($arrData);
    }

    /**
     * 获取下拉框选项
     * @param Request $request
     * @param WarehouseService $purchaseService
     * @param WarehouseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect(Request $request, WarehouseService $purchaseService, WarehouseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $arrData = $purchaseService->getSelect();
        return json($arrData);
    }

    public function secondWarehouse(Request $request, WarehouseService $purchaseService, WarehouseValidate $validate)
    {
        $disabled = $request->param('disabled', 'true');
        $disabled = $disabled === 'true';
        $param['status'] = $request->param('status', null);

        $arrData = $purchaseService->secondWarehouse($disabled, $param);
        return json($arrData);
    }

    public function firstWarehouse(Request $request, WarehouseService $purchaseService, WarehouseValidate $validate)
    {
        $checkData = $request->param();
        $param['status'] = $request->param('status', null);
        $arrData = $purchaseService->firstWarehouse($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request, WarehouseService $purchaseService)
    {
        $param = $request->param('field', null);
        $arrData = $purchaseService->getFieldEnum($param);
        return json($arrData);
    }

    public function getSelectEnum(Request $request, WarehouseService $purchaseService)
    {
        $arrData = $purchaseService->getSelectEnum();
        return json($arrData);
    }

    public function getSearchCity(Request $request, WarehouseService $purchaseService)
    {
        $param['name'] =  $request->param('name', null);
        $arrData = $purchaseService->getSearchCity($param);
        return json($arrData);
    }

    public function getCurrencySelect(Request $request, WarehouseService $purchaseService)
    {
        $param['wid'] =  $request->param('wid', null);
        $arrData = $purchaseService->getCurrencySelect($param);
        return json($arrData);
    }
}
